package com.xinchao.service.impl;

import com.xinchao.dao.DeviceInfoMapper;
import com.xinchao.exception.ServiceException;
import com.xinchao.model.DeviceInfo;
import com.xinchao.model.dto.CityDeviceSumDTO;
import com.xinchao.model.dto.CityRegionDeviceSumDTO;
import com.xinchao.service.DeviceInfoService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 终端信息Service实现类
 *
 * @author dxy
 * @date 2018/10/9 18:26
 */
@Service(value = "deviceInfoService")
public class DeviceInfoServiceImpl implements DeviceInfoService {
	@Autowired
	private DeviceInfoMapper deviceInfoMapper;

	/**
	 * 通过机器码获取终端信息
	 *
	 * @param deviceNumber 机器码
	 * @return DeviceInfo
	 * @throws ServiceException
	 */
	@Override
	public DeviceInfo getDeviceInfoByDeviceNumber(String deviceNumber) throws ServiceException {
		if (StringUtils.isBlank(deviceNumber)) {
			throw new ServiceException("机器码不能为空");
		}
		return deviceInfoMapper.getDeviceInfoByDeviceNumber(deviceNumber);
	}

	/**
	 * 获取所有终端
	 *
	 * @return List<DeviceInfo>
	 */
	@Override
	public List<DeviceInfo> listAllDeviceInfo() {
		return deviceInfoMapper.listAllDeviceInfo();
	}

	/**
	 * 获取所有终端编码
	 *
	 * @return List<String>
	 */
	@Override
	public List<String> listAllDeviceNumber() {
		return deviceInfoMapper.listAllDeviceNumber();
	}

	/**
	 * 获取城市终端总数
	 *
	 * @param deviceNumberList 机器码列表
	 * @return List<CityDeviceSumDTO>
	 */
	@Override
	public List<CityDeviceSumDTO> getCityDeviceSumPage(List<String> deviceNumberList) {
		List<CityDeviceSumDTO> resultList = new ArrayList<>();
		if (CollectionUtils.isEmpty(deviceNumberList)) {
			return resultList;
		}
		List<CityDeviceSumDTO> dtoList = deviceInfoMapper.getCityDeviceSumPage(deviceNumberList);
		if (CollectionUtils.isEmpty(dtoList)) {
			return resultList;
		}
		return dtoList;
	}

	/**
	 * 获取城市区县终端总数
	 *
	 * @param deviceNumberList 机器码列表
	 * @return List<CityRegionDeviceSumDTO>
	 */
	@Override
	public List<CityRegionDeviceSumDTO> getCityRegionDeviceSumPage(List<String> deviceNumberList) {
		List<CityRegionDeviceSumDTO> resultList = new ArrayList<>();
		if (CollectionUtils.isEmpty(deviceNumberList)) {
			return resultList;
		}
		List<CityRegionDeviceSumDTO> dtoList = deviceInfoMapper.getCityRegionDeviceSumPage(deviceNumberList);
		if (CollectionUtils.isEmpty(dtoList)) {
			return resultList;
		}
		return dtoList;
	}
}
